# 作业：
# 1、添加用户分组——》删除刚新增的用户分组
# 2、添加客服话术——》删除刚新增的客服话术
# 3、添加用户标签——》删除刚新增的用户标签
# 使用requests+pytest实现，每条用例要加上断言，对于新增的数据，要做关联
import requests
import pytest
import allure
@allure.feature("用户分组")
class TestCRMEB():
    @pytest.fixture(scope='function')
    def login(self):
        self.str_ = "http://123.56.16.34:8899/"
        response = requests.post(url=self.str_ +"adminapi/login",json={"account":"admin","pwd":"123456","imgcode":"a"})                                                                 #todo：登录接口
        self.token_ = response.json()['data']['token']                                                                                                                                  #todo:获取token
    @allure.story("添加用户分组")
    def test_add(self,login):
        response_add = requests.post(url=self.str_+"adminapi/user/user_group/save.html",headers={"Authori-zation":"Bearer " + str(self.token_)},json={"group_name":"测试分组"})            #todo:增加分组
        print(response_add.json())                                                                                                                                                        #todo:提交成功
        assert response_add.json()['status'] == 200                                                                                                                                      #todo:断言200
        response_add_save = requests.get(url=self.str_+"adminapi/user/user_group/list?page=1&limit=15",headers={"Authori-zation":"Bearer " + str(self.token_)})                          #todo：保存增加的分组并查看
        print(response_add_save.json())                                                                                                                                                  #todo:查看增加结果
        assert response_add_save.json()['status'] == 200
        for  i in response_add_save.json()['data']['list']:
            global id_second
            id_second = i['id']                                                                                                                                                             #todo：获取加入分组的id

    @allure.story("删除用户分组")
    def test_delete(self,login):
        responde_delete1 = requests.delete(url=self.str_+"/adminapi/user/user_group/del/"+str(id_second),headers={"Authori-zation":"Bearer " + str(self.token_)})                        #todo:删除刚加入的分组id
        print(responde_delete1.json())                                                                                                                                                   #todo：输出删除结果
        assert responde_delete1.json()['status'] == 200
        response_delete_select = requests.get(url=self.str_+"adminapi/user/user_group/list?page=1&limit=15",headers={"Authori-zation":"Bearer "+ str(self.token_)})                      #todo：获取删除结果
        print(response_delete_select.json())
        assert response_delete_select.json()['status'] == 200

    @allure.story("增加话术")
    def test_add_script(self,login):
        response_script_add = requests.post(url=self.str_ + "adminapi/app/wechat/speechcraft",
                                            headers={"Authori-zation": "Bearer " + str(self.token_)},
                                            json={"title": "测试话术", "message": "测试话术"})                                                                                           # todo:增加话术
        print(response_script_add.json())                                                                                                                                              # todo：查看话术是否成功
        assert response_script_add.json()['status'] == 200
        response_script_add_select = requests.get(url=self.str_ + "adminapi/app/wechat/speechcraft?page=1&limit=15&cate_id=",
                                                  headers={"Authori-zation": "Bearer " + str(self.token_)})                                                                          # todo:查看所有话术
        print(response_script_add_select.json())
        assert response_script_add_select.json()['status'] == 200
        for i in response_script_add_select.json()['data']['list']:
            global scropt_id
            scropt_id = i['id']

    @allure.story("删除话术")
    def test_delete_script(self,login):
        response_script_delect = requests.delete(url=self.str_ + "adminapi/app/wechat/speechcraft/" + str(scropt_id),
                                                 headers={"Authori-zation": "Bearer " + str(self.token_)})                                                                          # todo:删除话术
        print(response_script_delect.json())
        assert response_script_delect.json()['status'] == 200

    @allure.story("增加标签")
    def test_add_label(self,login):
        response_add_label = requests.post(url=self.str_ + "adminapi/user/user_label/save.html",
                                           headers={"Authori-zation": "Bearer " + str(self.token_)},
                                           json={"label_cate": 97, "label_name": "测试标签"})                                                                                       # todo:增加标签
        print(response_add_label.json())
        assert response_add_label.json()['status'] == 200
        response_select_label = requests.get(url=self.str_ + "adminapi/user/user_label?page=2&limit=15&label_cate=",
                                             headers={"Authori-zation": "Bearer " + str(self.token_)})
        print(response_select_label.json())                                                                                                                                          # todo:查看标签
        assert response_select_label.json()['status'] == 200
        for i in response_select_label.json()['data']['list']:
            global label_id
            label_id = i['id']

    @allure.story("删除标签")
    def test_delete_lable(self,login):
        response_delete_label = requests.delete(url=self.str_ + "adminapi/user/user_label/del/" + str(label_id),
                                                headers={"Authori-zation": "Bearer " + str(self.token_)})                                                                            # todo:删除标签
        print(response_delete_label.json())
        assert response_delete_label.json()['status'] == 200
if __name__ ==  '__main__':
    pytest.main()
